<template>
  <div class="app-container">
    <el-button type="primary" @click="addMenu">添加</el-button>
    <el-button type="primary" @click="fetchData">刷新</el-button>
    <!-- default-expand-all -->
    <el-table
      v-loading="listLoading"
      :data="menuGridData"
      :tree-props="{children: 'children'}"
      row-key="menuId"
      border
      style="width: 100%"
    >
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdateShow(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column fixed prop="menuParentName" label="上级菜单" width="150" />
      <el-table-column prop="menuName" label="名称" width="120" />
      <el-table-column prop="menuUrl" label="Url" width="120" />
      <el-table-column prop="menuPath" label="页面路径" width="120" />
      <el-table-column prop="menuAuth" label="权限" width="300" />
      <el-table-column prop="menuType" label="类型" width="120" />
      <el-table-column prop="menuIcon" label="图标" width="100" />
      <el-table-column prop="menuOrder" label="排序" width="100" />
    </el-table>
    <menu-edit ref="form" @fetchData="fetchData" />
  </div>
</template>

<script>
import { getAllMenu, deleteMenu } from '@/api/sys/menu'
import { getTargetKeyValueFromSource } from '@/utils/easyUtils'
import MenuEdit from '@/views/menu/MenuEdit'

export default {
  components: {
    MenuEdit
  },
  data() {
    return {
      listLoading: true,
      menuGridData: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.menuGridData = []
      this.listLoading = true
      getAllMenu()
        .then(response => {
          this.menuGridData = response.data
          this.listLoading = false
          this.$refs.form.getMenuTreeList(response.data)
        })
        .catch(err => {
          this.listLoading = false
          console.log(err)
        })
    },
    handleUpdateShow(row) {
      this.$refs.form.formData = getTargetKeyValueFromSource(this.$refs.form.formData, row)
      this.$refs.form.dialogFormVisible = true
    },
    handleDelete(row) {
      this.$confirm('确定删除菜单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMenu({ 'menuId': row.menuId }).then(response => {
          this.fetchData(1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addMenu() {
      this.$refs.form.formData = getTargetKeyValueFromSource(this.$refs.form.formData, {
        menuParentId: 0,
        menuId: 0,
        menuName: '',
        menuUrl: '',
        menuPath: '',
        menuType: '',
        menuAuth: '',
        menuOrder: '',
        menuIcon: ''
      })
      this.$refs.form.dialogFormVisible = true
    }
  }
}
</script>
